package cn.xpg.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import cn.xpg.entity.Order;
import cn.xpg.service.OrderService;
import cn.xpg.tools.PageSupport;

@Controller
public class OrderController {

	@Resource(name = "orderService")
	private OrderService orderService;

	// 分页查询订单
	@RequestMapping("/getOrderList.do")
	public String getOrderList(HttpServletRequest request) {
		String pageIndex = request.getParameter("pageIndex");
		String order_id = request.getParameter("order_id");
		String order_createtime = request.getParameter("order_createtime");
		String order_updatetime = request.getParameter("order_updatetime");
		int pageSize = 3;
		int currentPageNo = 1;

		if (pageIndex != null) {
			try {
				currentPageNo = Integer.valueOf(pageIndex);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		int totalCount = 0;
		totalCount = orderService.getOrderCount(order_id, order_createtime, order_updatetime);
		System.out.println("查询订单信息！！！！！！！！！！————>getOrderList");
		PageSupport pages = new PageSupport();
		pages.setCurrentPageNo(currentPageNo);
		pages.setPageSize(pageSize);
		pages.setTotalCount(totalCount);

		int totalPageCount = pages.getTotalPageCount();
		if (currentPageNo < 1) {
			currentPageNo = 1;
		} else if (currentPageNo > totalPageCount) {
			currentPageNo = totalPageCount;
		}

		List<Order> orderList = null;
		orderList = orderService.getOrderList(order_id, order_createtime, order_updatetime,
				(currentPageNo - 1) * pageSize, pageSize);
		for (Order order : orderList) {
			System.out.println("!!!!!!!!!!!!!" + order.getOrder_id());
			System.out.println("!!!!!!!!!!!!!" + order.getPay_type());
		}

		request.setAttribute("orderList", orderList);
		request.setAttribute("totalPageCount", totalPageCount);
		request.setAttribute("pages", pages);
		request.setAttribute("order_id", order_id);
		request.setAttribute("order_createtime", order_createtime);
		request.setAttribute("order_updatetime", order_updatetime);
		return "/jsp/repair/group_repair_list";
	}

	// 新增订单
	@RequestMapping("addOrder.do")
	public String addOrder(@Valid Order order) {
		System.out.println("新增订单信息！！！！！！！！！！————>addorder");
		/*String bh=new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
		order.setOrder_id(bh);*/
		if (orderService.addOrder(order) > 0) {
			return "redirect:/getOrderList.do";
		} else {
			return "/jsp/repair/group_repair_list";
		}
	}

	// 获取订单Id
	@RequestMapping("getOrderId.do")
	public String getOrderId(HttpServletRequest request) {
		System.out.println("获取订单Id！！！！！！！！！！————>getOrderId");
		int order_id = Integer.parseInt(request.getParameter("order_id"));
		Order order = orderService.getOrderId(order_id);
		request.setAttribute("order", order);
		return "/jsp/base/order_update";
	}

	// 修改订单
	@RequestMapping("updateOrder.do")
	public String updateOrder(@Valid Order order, HttpServletRequest request) {
		System.out.println("修改订单信息！！！！！！！！！！————>updateOrder");
		if (orderService.updateOrder(order) > 0) {
			System.out.println("修改成功！！！！！！");
			return "redirect:/getOrderList.do";
		} else {
			System.out.println("修改失败！！！！！！");
			request.setAttribute("order", order);
			return "/jsp/base/order_update";
		}
	}

	// 删除订单
	@RequestMapping("deleteOrder.do")
	public String deleteOrder(HttpServletRequest request) {
		System.out.println("删除订单信息！！！！！！！！！！————>deleteOrder");
		int order_id = Integer.valueOf(request.getParameter("order_id"));
		if (orderService.deleteOrder(order_id) > 0) {
			System.out.println("删除成功！！！！！！");
		} else {
			System.out.println("删除失败！！！！！！");
		}
		return "redirect:/getOrderList.do";
	}

 

	// 打印
	@RequestMapping("printer.do")
	public String printer(HttpServletRequest request) {

		return "/jsp/repair/repair_printer";
	}
}
